{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Biome Level statistics for model: WK2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import the libraries\n",
    "import ee\n",
    "import pandas as pd\n",
    "import os\n",
    "import numpy as np\n",
    "import random\n",
    "from random import sample\n",
    "import itertools \n",
    "import geopandas as gpd\n",
    "from sklearn.metrics import r2_score\n",
    "from termcolor import colored # this is allocate colour and fonts type for the print title and text\n",
    "from IPython.display import display, HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#set the working directory of local drive for Grid search result table loading\n",
    "# os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# initialize the earth engine API\n",
    "ee.Initialize()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1 Load the required composites and images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load the basic maps that needed for the analysis\n",
    "# load the carbon concentration map\n",
    "carbonConcentration = ee.Image(\"users/leonidmoore/ForestBiomass/Biome_level_Wood_Carbon_Conentration_Map\")\n",
    "# load the two composites tha will be used in the analysis\n",
    "compositeImage =ee.Image(\"users/leonidmoore/ForestBiomass/20200915_Forest_Biomass_Predictors_Image\")\n",
    "compositeImageNew = ee.Image(\"projects/crowtherlab/Composite/CrowtherLab_Composite_30ArcSec\")\n",
    "# load the biome layer \n",
    "biomeLayer = compositeImage.select(\"WWF_Biome\")\n",
    "# define a pixel area layer with unit km2\n",
    "pixelAreaMap = ee.Image.pixelArea().divide(10000);\n",
    "# define the boundary geography reference\n",
    "unboundedGeo = ee.Geometry.Polygon([-180, 88, 0, 88, 180, 88, 180, -88, 0, -88, -180, -88], None, False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2 Load the biomass density maps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load the carbon density layers\n",
    "potentialDensity = ee.Image(\"users/nordmannmoore/ForestBiomass/RemoteSensingModel/EnsambleMaps/Predicted_WK2_Potential_density_Ensambled_Mean\").unmask()\n",
    "presentDensity =  ee.Image(\"users/leonidmoore/ForestBiomass/WalkerMap/reprojected_Walker_map_1km\").unmask()\n",
    "\n",
    "# define the standard projection\n",
    "stdProj = potentialDensity.projection();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3 Adjust the present and potential density maps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load the present and potential forest cover\n",
    "presentForestCover = compositeImage.select('PresentTreeCover').unmask() # uniform with potential in the  0-1 scale\n",
    "potentialCoverAdjusted = ee.Image(\"users/leonidmoore/ForestBiomass/Bastin_et_al_2019_Potential_Forest_Cover_Adjusted\").unmask().rename('PotentialForestCover')\n",
    "# define the present and potential forest cover masks\n",
    "presentMask= presentForestCover.gt(0)\n",
    "potentialMask= potentialCoverAdjusted.gt(0)\n",
    "\n",
    "# check the difference of the two density maps\n",
    "potentialHigher = potentialDensity.multiply(pixelAreaMap).subtract(presentDensity.multiply(pixelAreaMap)).gte(0)\n",
    "potentialLower = potentialDensity.multiply(pixelAreaMap).subtract(presentDensity.multiply(pixelAreaMap)).lt(0)\n",
    "# replace the lower potential value by present biomass density value\n",
    "agbPotentialDensity = presentDensity.multiply(potentialLower).add(potentialDensity.multiply(potentialHigher))\n",
    "agbPresentDensity = presentDensity"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4 Partioning the potential cover into different landuse types"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load all the landuse type layers\n",
    "croplandOrg = ee.Image(\"users/leonidmoore/ForestBiomass/HYDE31/cropland_Percent\").rename('cropland').divide(100).reproject(crs=stdProj);\n",
    "grazingOrg = ee.Image(\"users/leonidmoore/ForestBiomass/HYDE31/grazing_Percent\").rename('grazing').divide(100).reproject(crs=stdProj);\n",
    "pastureOrg = ee.Image(\"users/leonidmoore/ForestBiomass/HYDE31/pasture_Percent\").rename('pasture').divide(100).reproject(crs=stdProj);\n",
    "rangelandOrg = ee.Image(\"users/leonidmoore/ForestBiomass/HYDE31/rangeland_Percent\").rename('rangeland').divide(100).reproject(crs=stdProj);\n",
    "urbanOrg = compositeImage.select(['LandCoverClass_Urban_Builtup']).divide(100).unmask().reproject(crs=stdProj);\n",
    "snowIceOrg = compositeImageNew.select(['ConsensusLandCoverClass_Snow_Ice']).divide(100).unmask().reproject(crs=stdProj);\n",
    "openWaterOrg = compositeImageNew.select(['ConsensusLandCoverClass_Open_Water']).divide(100).unmask().reproject(crs=stdProj);\n",
    "# define the total landcover types\n",
    "sumCover = presentForestCover.add(pastureOrg).add(rangelandOrg).add(croplandOrg).add(urbanOrg).add(openWaterOrg).add(snowIceOrg);\n",
    "oneSubtract = ee.Image(1).subtract(sumCover);\n",
    "freeland = oneSubtract.multiply(oneSubtract.gte(0));\n",
    "# get the scale ratio for pixels with sumCover larger than 1\n",
    "scaleRatio = ee.Image(1).subtract(presentForestCover).divide(sumCover.subtract(presentForestCover)).multiply(oneSubtract.lt(0));\n",
    "# get the ratio of these three disturbed maps\n",
    "pasture = pastureOrg.multiply(scaleRatio).multiply(oneSubtract.lt(0)).add(pastureOrg.multiply(oneSubtract.gte(0))).unmask();\n",
    "rangeland = rangelandOrg.multiply(scaleRatio).multiply(oneSubtract.lt(0)).add(rangelandOrg.multiply(oneSubtract.gte(0))).unmask();\n",
    "cropland = croplandOrg.multiply(scaleRatio).multiply(oneSubtract.lt(0)).add(croplandOrg.multiply(oneSubtract.gte(0))).unmask();\n",
    "urban = urbanOrg.multiply(scaleRatio).multiply(oneSubtract.lt(0)).add(urbanOrg.multiply(oneSubtract.gte(0))).unmask();\n",
    "openWater = openWaterOrg.multiply(scaleRatio).multiply(oneSubtract.lt(0)).add(openWaterOrg.multiply(oneSubtract.gte(0))).unmask();\n",
    "snowIce = snowIceOrg.multiply(scaleRatio).multiply(oneSubtract.lt(0)).add(snowIceOrg.multiply(oneSubtract.gte(0))).unmask();\n",
    "sumTT = presentForestCover.add(pasture).add(rangeland).add(cropland).add(urban).add(freeland).add(openWater).add(snowIce).unmask();\n",
    "\n",
    "effectivePotentialMask = freeland.add(rangeland).add(pasture).add(cropland).add(urban).gt(0);\n",
    "# there are some pixels without any landcover survived but with open water and ice and snow. here we mask these pixels out\n",
    "sumlandCover = pastureOrg.add(rangelandOrg).add(croplandOrg).add(urbanOrg).add(freeland);\n",
    "restorationMap = potentialCoverAdjusted.subtract(presentForestCover).mask(effectivePotentialMask).unmask();\n",
    "\n",
    "# sum all these scaled layersv\n",
    "scaledSum = pasture.add(rangeland).add(cropland).add(urban).add(freeland);\n",
    "potentialCoverFinal = restorationMap.add(presentForestCover);\n",
    "# allocate the potential equally to each layer\n",
    "freelandPotentialCover = freeland.divide(scaledSum).multiply(restorationMap).unmask();\n",
    "rangelandPotentialCover = rangeland.divide(scaledSum).multiply(restorationMap).unmask();\n",
    "pasturePotentialCover = pasture.divide(scaledSum).multiply(restorationMap).unmask();\n",
    "croplandPotentialCover = cropland.divide(scaledSum).multiply(restorationMap).unmask();\n",
    "urbanPotentialCover = urban.divide(scaledSum).multiply(restorationMap).unmask();\n",
    "#  allocate the freeland potential in pixels with forest cover larger than 10% to conservation potential\n",
    "freelandForConsevation = freelandPotentialCover.multiply(presentForestCover.gte(0.1)).unmask();\n",
    "maximumPotentialCover = freelandForConsevation.add(presentForestCover);\n",
    "# calucate the reall freeland outside of forest\n",
    "freelandLeftMap = freelandPotentialCover.subtract(freelandForConsevation).unmask()# the left positive pixels are real freeland pixels"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5 Partioning the biomass potential into different landuse types"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# calculate the existing carbon, present potential carbon and absolute potential carbon in forests\n",
    "absoluteImage1 = agbPresentDensity.multiply(pixelAreaMap).multiply(presentMask).divide(1000000000).rename('Present')\n",
    "absoluteImage3 = agbPotentialDensity.multiply(pixelAreaMap).multiply(potentialCoverFinal.gt(0)).divide(1000000000).rename('AbsolutePotential')\n",
    "# get the sum of the potential covers\n",
    "potentialCoverSum = freelandLeftMap.add(rangelandPotentialCover).add(pasturePotentialCover).add(croplandPotentialCover).add(urbanPotentialCover)\n",
    "\n",
    "trueRestorationPotential = absoluteImage3.subtract(absoluteImage1).multiply(1000000000)\n",
    "ratioPotentialBiomassDensity = absoluteImage1.multiply(potentialCoverFinal.divide(presentForestCover))\n",
    "#  get the real for conservation potential\n",
    "realDensityIncreased = absoluteImage3.subtract(absoluteImage1).mask(absoluteImage3.subtract(ratioPotentialBiomassDensity).gt(0)).unmask()\n",
    "realDensityNotIncreased = absoluteImage3.subtract(absoluteImage1).mask(absoluteImage3.subtract(ratioPotentialBiomassDensity).lte(0)).unmask()\n",
    "trueReforestationPotential = realDensityNotIncreased.add(realDensityIncreased.multiply(ee.Image(1).subtract(presentForestCover.divide(potentialCoverFinal))))\n",
    "\n",
    "conservationPotentialPart1 = realDensityIncreased.multiply(presentForestCover.add(freelandForConsevation).divide(potentialCoverFinal))\n",
    "conservationPotentialPart2 = realDensityNotIncreased.multiply(freelandForConsevation.divide(potentialCoverFinal.subtract(presentForestCover)))\n",
    "\n",
    "# calculate the part of the potential inside the forest cover which was allocate to conservation potential.\n",
    "freelandForConservation1 = realDensityIncreased.multiply(freelandForConsevation.divide(potentialCoverFinal))\n",
    "freelandForConservation = freelandForConservation1.add(conservationPotentialPart2).rename('FreeToConservation')\n",
    "\n",
    "absoluteImage2 = conservationPotentialPart1.add(conservationPotentialPart2).add(absoluteImage1).rename('PresentPotential')\n",
    "\n",
    "trueReforestationPotential = absoluteImage3.subtract(absoluteImage2)\n",
    "\n",
    "absoluteImage4 = trueReforestationPotential.multiply(freelandLeftMap.divide(potentialCoverSum)).rename('FreelandPotential')\n",
    "absoluteImage5 = trueReforestationPotential.multiply(rangelandPotentialCover.divide(potentialCoverSum)).rename('RangelandPotential')\n",
    "absoluteImage6 = trueReforestationPotential.multiply(pasturePotentialCover.divide(potentialCoverSum)).rename('PasturePotential')\n",
    "absoluteImage7 = trueReforestationPotential.multiply(croplandPotentialCover.divide(potentialCoverSum)).rename('CroplandPotential')\n",
    "absoluteImage8 = trueReforestationPotential.multiply(urbanPotentialCover.divide(potentialCoverSum)).rename('UrbanPotential')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Calculate the potential numbers and write into local folder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m\u001b[34mThe biomass partition results in biome: \n",
      "\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Present</th>\n",
       "      <th>PresentPotential</th>\n",
       "      <th>AbsolutePotential</th>\n",
       "      <th>FreelandPotential</th>\n",
       "      <th>RangelandPotential</th>\n",
       "      <th>PasturePotential</th>\n",
       "      <th>CroplandPotential</th>\n",
       "      <th>UrbanPotential</th>\n",
       "      <th>FreeToConservation</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>180.679087</td>\n",
       "      <td>206.324690</td>\n",
       "      <td>227.495188</td>\n",
       "      <td>4.196232</td>\n",
       "      <td>0.072961</td>\n",
       "      <td>6.906048</td>\n",
       "      <td>9.694124</td>\n",
       "      <td>0.301134</td>\n",
       "      <td>5.641849</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>7.853757</td>\n",
       "      <td>10.163066</td>\n",
       "      <td>18.318267</td>\n",
       "      <td>2.056857</td>\n",
       "      <td>0.018983</td>\n",
       "      <td>1.722267</td>\n",
       "      <td>4.298879</td>\n",
       "      <td>0.058215</td>\n",
       "      <td>0.676396</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.759283</td>\n",
       "      <td>4.150534</td>\n",
       "      <td>5.226569</td>\n",
       "      <td>0.389364</td>\n",
       "      <td>0.023104</td>\n",
       "      <td>0.382390</td>\n",
       "      <td>0.276907</td>\n",
       "      <td>0.004271</td>\n",
       "      <td>0.441382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>39.247761</td>\n",
       "      <td>45.311131</td>\n",
       "      <td>61.425710</td>\n",
       "      <td>3.832867</td>\n",
       "      <td>0.020854</td>\n",
       "      <td>4.510046</td>\n",
       "      <td>7.310088</td>\n",
       "      <td>0.440724</td>\n",
       "      <td>2.014419</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>19.270000</td>\n",
       "      <td>21.136341</td>\n",
       "      <td>23.371004</td>\n",
       "      <td>1.008632</td>\n",
       "      <td>0.032803</td>\n",
       "      <td>0.684647</td>\n",
       "      <td>0.464136</td>\n",
       "      <td>0.044444</td>\n",
       "      <td>0.846033</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>35.712276</td>\n",
       "      <td>42.349494</td>\n",
       "      <td>44.724015</td>\n",
       "      <td>2.047880</td>\n",
       "      <td>0.000735</td>\n",
       "      <td>0.162923</td>\n",
       "      <td>0.151807</td>\n",
       "      <td>0.011175</td>\n",
       "      <td>2.782948</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>44.326087</td>\n",
       "      <td>59.639832</td>\n",
       "      <td>93.712322</td>\n",
       "      <td>7.635575</td>\n",
       "      <td>11.823890</td>\n",
       "      <td>8.256365</td>\n",
       "      <td>6.283224</td>\n",
       "      <td>0.073436</td>\n",
       "      <td>4.096229</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>3.737922</td>\n",
       "      <td>5.120637</td>\n",
       "      <td>22.506603</td>\n",
       "      <td>3.118732</td>\n",
       "      <td>5.676980</td>\n",
       "      <td>2.239005</td>\n",
       "      <td>6.250535</td>\n",
       "      <td>0.100713</td>\n",
       "      <td>0.176205</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2.160503</td>\n",
       "      <td>2.794880</td>\n",
       "      <td>3.843621</td>\n",
       "      <td>0.319122</td>\n",
       "      <td>0.342355</td>\n",
       "      <td>0.202123</td>\n",
       "      <td>0.174308</td>\n",
       "      <td>0.010834</td>\n",
       "      <td>0.105520</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>3.424521</td>\n",
       "      <td>4.812063</td>\n",
       "      <td>10.135409</td>\n",
       "      <td>1.476026</td>\n",
       "      <td>2.357953</td>\n",
       "      <td>0.715993</td>\n",
       "      <td>0.761088</td>\n",
       "      <td>0.012287</td>\n",
       "      <td>0.181741</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>3.415699</td>\n",
       "      <td>5.444996</td>\n",
       "      <td>8.868815</td>\n",
       "      <td>3.417675</td>\n",
       "      <td>0.001211</td>\n",
       "      <td>0.000417</td>\n",
       "      <td>0.004262</td>\n",
       "      <td>0.000254</td>\n",
       "      <td>0.248007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>3.719553</td>\n",
       "      <td>4.480091</td>\n",
       "      <td>8.986009</td>\n",
       "      <td>1.197383</td>\n",
       "      <td>0.018457</td>\n",
       "      <td>1.573203</td>\n",
       "      <td>1.641228</td>\n",
       "      <td>0.075648</td>\n",
       "      <td>0.219889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>2.046002</td>\n",
       "      <td>3.156571</td>\n",
       "      <td>28.944531</td>\n",
       "      <td>12.496586</td>\n",
       "      <td>9.832031</td>\n",
       "      <td>0.902384</td>\n",
       "      <td>2.487412</td>\n",
       "      <td>0.069548</td>\n",
       "      <td>0.251792</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>1.787585</td>\n",
       "      <td>2.261863</td>\n",
       "      <td>2.687415</td>\n",
       "      <td>0.156645</td>\n",
       "      <td>0.001183</td>\n",
       "      <td>0.071249</td>\n",
       "      <td>0.182894</td>\n",
       "      <td>0.013582</td>\n",
       "      <td>0.114525</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sum</th>\n",
       "      <td>350.140036</td>\n",
       "      <td>417.146187</td>\n",
       "      <td>560.245479</td>\n",
       "      <td>43.349575</td>\n",
       "      <td>30.223501</td>\n",
       "      <td>28.329060</td>\n",
       "      <td>39.980891</td>\n",
       "      <td>1.216264</td>\n",
       "      <td>17.796935</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Present  PresentPotential  AbsolutePotential  FreelandPotential  \\\n",
       "0    180.679087        206.324690         227.495188           4.196232   \n",
       "1      7.853757         10.163066          18.318267           2.056857   \n",
       "2      2.759283          4.150534           5.226569           0.389364   \n",
       "3     39.247761         45.311131          61.425710           3.832867   \n",
       "4     19.270000         21.136341          23.371004           1.008632   \n",
       "5     35.712276         42.349494          44.724015           2.047880   \n",
       "6     44.326087         59.639832          93.712322           7.635575   \n",
       "7      3.737922          5.120637          22.506603           3.118732   \n",
       "8      2.160503          2.794880           3.843621           0.319122   \n",
       "9      3.424521          4.812063          10.135409           1.476026   \n",
       "10     3.415699          5.444996           8.868815           3.417675   \n",
       "11     3.719553          4.480091           8.986009           1.197383   \n",
       "12     2.046002          3.156571          28.944531          12.496586   \n",
       "13     1.787585          2.261863           2.687415           0.156645   \n",
       "sum  350.140036        417.146187         560.245479          43.349575   \n",
       "\n",
       "     RangelandPotential  PasturePotential  CroplandPotential  UrbanPotential  \\\n",
       "0              0.072961          6.906048           9.694124        0.301134   \n",
       "1              0.018983          1.722267           4.298879        0.058215   \n",
       "2              0.023104          0.382390           0.276907        0.004271   \n",
       "3              0.020854          4.510046           7.310088        0.440724   \n",
       "4              0.032803          0.684647           0.464136        0.044444   \n",
       "5              0.000735          0.162923           0.151807        0.011175   \n",
       "6             11.823890          8.256365           6.283224        0.073436   \n",
       "7              5.676980          2.239005           6.250535        0.100713   \n",
       "8              0.342355          0.202123           0.174308        0.010834   \n",
       "9              2.357953          0.715993           0.761088        0.012287   \n",
       "10             0.001211          0.000417           0.004262        0.000254   \n",
       "11             0.018457          1.573203           1.641228        0.075648   \n",
       "12             9.832031          0.902384           2.487412        0.069548   \n",
       "13             0.001183          0.071249           0.182894        0.013582   \n",
       "sum           30.223501         28.329060          39.980891        1.216264   \n",
       "\n",
       "     FreeToConservation  \n",
       "0              5.641849  \n",
       "1              0.676396  \n",
       "2              0.441382  \n",
       "3              2.014419  \n",
       "4              0.846033  \n",
       "5              2.782948  \n",
       "6              4.096229  \n",
       "7              0.176205  \n",
       "8              0.105520  \n",
       "9              0.181741  \n",
       "10             0.248007  \n",
       "11             0.219889  \n",
       "12             0.251792  \n",
       "13             0.114525  \n",
       "sum           17.796935  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Stack the absolute biomass layers into an Image.\n",
    "absPotentialImage = absoluteImage1.addBands(absoluteImage2).addBands(absoluteImage3).addBands(absoluteImage4).addBands(absoluteImage5).addBands(absoluteImage6).addBands(absoluteImage7).addBands(absoluteImage8).addBands(freelandForConservation)\n",
    "# define the function to do the biome level statistics which could be applied by map      \n",
    "def biomeLevelStat(biome):\n",
    "    biomeMask = biomeLayer.eq(ee.Number(biome))\n",
    "    masked_img = absPotentialImage.mask(biomeMask)\n",
    "    output = masked_img.reduceRegion(reducer= ee.Reducer.sum(),\n",
    "                                     geometry= unboundedGeo,\n",
    "                                     crs='EPSG:4326',\n",
    "                                     crsTransform=[0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                     maxPixels= 1e13)\n",
    "    return output#.getInfo().get('Present')\n",
    "\n",
    "\n",
    "biomeList = ee.List([1,2,3,4,5,6,7,8,9,10,11,12,13,14])\n",
    "statisticTable = biomeList.map(biomeLevelStat).getInfo()\n",
    "# transform into data frame\n",
    "outputTable = pd.DataFrame(statisticTable,columns =['Present','PresentPotential','AbsolutePotential','FreelandPotential','RangelandPotential','PasturePotential','CroplandPotential','UrbanPotential','FreeToConservation'])#.round(1)\n",
    "outputTable.loc['sum'] = outputTable.sum() \n",
    "outputTable.to_csv('Data/BiomeLevelStatistics/StatisticsForModels/AGB_WK2_Biome_Level_Statistics.csv',header=True,mode='w+')\n",
    "# display the output of the carbon partitioning\n",
    "print(colored('The biomass partition results in biome: \\n', 'blue', attrs=['bold']))\n",
    "outputTable.head(15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# If you got the error 'EEException: Too many concurrent aggregations.', please re-run this chunck of code again."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
